package cn.stylefeng.guns.modular.gridsystem.timer;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.core.timer.TimerTaskRunner;
import cn.stylefeng.guns.modular.gridsystem.entity.*;
import cn.stylefeng.guns.modular.gridsystem.mqtt.MqttTopicConstant;
import cn.stylefeng.guns.modular.gridsystem.mqtt.MyMqttClient;
import cn.stylefeng.guns.modular.gridsystem.mqtt.result.FaultWarning;
import cn.stylefeng.guns.modular.gridsystem.service.*;
import cn.stylefeng.guns.modular.gridsystem.utils.MyConst;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.netty.handler.codec.mqtt.MqttProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 上报故障恢复正常通知
 *
 * @author ssy
 * @date 2023/3/16 11:23
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class MqttTaskRunner implements TimerTaskRunner {

    private final BizExceptionService bizExceptionService;
    private final BizExceptionLogService bizExceptionLogService;
    private final BizDeviceService bizDeviceService;
    private final BizStationService bizStationService;
    private final BizGatewayService bizGatewayService;
    private final BizGatewayChannelService bizGatewayChannelService;
    private final MyMqttClient myMqttClient;
  //  private final MqttProperties mqttProperties;

    @Override
    public void action() {

        if (false) {
            LambdaQueryWrapper<BizExceptionLog> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByDesc(BizExceptionLog::getCreateTime);
            queryWrapper.last("limit 1");
            BizExceptionLog exceptionLog = bizExceptionLogService.getOne(queryWrapper);
            if (ObjectUtil.isNotNull(exceptionLog)) {
                Integer exceptionId = exceptionLog.getExceptionId();
                final BizException exception = bizExceptionService.getById(exceptionId);
                if (ObjectUtil.isNull(exception)) {
                    throw new RuntimeException("未查询到故障信息");
                }
                final Integer deviceId = exception.getDeviceId();

                final BizDevice bizDevice = bizDeviceService.getById(deviceId);
                if (ObjectUtil.isNull(bizDevice)) {
                    throw new RuntimeException(MyConst.DEVICE_NOT_EXIST);
                }

                final Integer stationId = bizDevice.getStationId();
                final BizStation bizStation = bizStationService.getById(stationId);

                if (ObjectUtil.isNull(bizStation)) {
                    throw new RuntimeException(MyConst.STATION_NOT_EXIST);
                }

                final BizGateway bizGateway = bizGatewayService.listByDeviceCode(exceptionLog.getGatewayCode());

                if (ObjectUtil.isNull(bizGateway)) {
                    throw new RuntimeException(MyConst.GATEWAY_NOT_EXIST);
                }

                final BizGatewayChannel bizGatewayChannel = bizGatewayChannelService.listByGatewayId(bizGateway.getId(), exceptionLog.getGatewayChannel());
                if (ObjectUtil.isNull(bizGatewayChannel)) {
                    throw new RuntimeException(MyConst.CHANNEL_NOT_EXIST);
                }

                //如果最新故障时间大于等于30分钟并且该故障为未处理时，将上报设备为正常状态，并且将该故障改为已处理状态
                if(DateUtil.between(exceptionLog.getExceptionTime(),DateUtil.date(), DateUnit.MINUTE) >= 30 && exception.getStatus() == 0){
                    FaultWarning faultWarning = new FaultWarning();
                    faultWarning.setStationName(bizStation.getStationName());
                    faultWarning.setWarnName("");
                    faultWarning.setStationAddress(bizStation.getStationAddress());
                    faultWarning.setGatewayCode(exceptionLog.getGatewayCode());
                    faultWarning.setGatewayChannel(exceptionLog.getGatewayChannel());
                    faultWarning.setDeviceId(deviceId);
                    faultWarning.setDeviceName(bizDevice.getDeviceName());
                    faultWarning.setVoiceUrl(exceptionLog.getFileName());
                    faultWarning.setInstallLocation(bizGatewayChannel.getInstallLocation());
                    faultWarning.setDateTime(DateUtil.now());
                    faultWarning.setScore(exceptionLog.getVoiceScore());
                    faultWarning.setErrorType(exception.getErrorType());
                    myMqttClient.publish(MqttTopicConstant.ALARM_PUSH, JSONUtil.toJsonStr(faultWarning));

                    final LambdaUpdateWrapper<BizException> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.set(BizException::getStatus,1)
                            .eq(BizException::getId,exception.getId());
                    bizExceptionService.update(updateWrapper);
                }

            }
        }

    }

}
